import global from 'global';
import forEach from 'lodash/forEach';
import {inBrowser} from 'willow-utils/es/commonUtil';
import {getFromLocal, set2Local} from 'willow-utils/es/storageUtil';
import {LANG, lang_abbr_enName_map, lang_abbr_localName_map, lang_abbr_zhName_map, LANGUAGE} from '../dataSources/i18n/i18n.def';

export const I18N_KEY = {
	LANGUAGE: `MEO_UI.I18N.LANGUAGE`,
	I18N_RESOURCE: `JZT.I18N.I18N_RESOURCE.`,
};

export {LANGUAGE, lang_abbr_zhName_map, lang_abbr_enName_map, lang_abbr_localName_map};

/**
 * @deprecated 无用, abbr 均为标准缩写
 */
export const lang_abbr_ISO_map = Object.fromEntries(Object.values(LANGUAGE).map(item => [item, item]));

// 获取当前浏览器的语种
export function getBrowserLang() {
	if (inBrowser()) {
		let language = navigator.language.toLowerCase();
		for (let [name, abbr] of Object.entries(LANGUAGE)) {
			if (language.includes(abbr)) return abbr;
		}
	}
	return LANGUAGE.英语;
}

// 获取当前语种
export function getCurrentLang(): LANGUAGE {
	return getFromLocal(I18N_KEY.LANGUAGE) || getBrowserLang();
}

// 设置当前语种
export function setCurrentLang(lang: LANG): void {
	return set2Local(I18N_KEY.LANGUAGE, lang);
}

// 获取语言位(test)
export function getLangBit(lang: LANG): number {
	return 1 << Object.values(LANGUAGE).indexOf(lang as LANGUAGE);
}

// 当前语言是否是 rtl 布局
export function isRtl(lang: LANG = getCurrentLang()): boolean {
	return [LANGUAGE.阿拉伯语, LANGUAGE.波斯语].includes(lang as LANGUAGE);
}

/**
 * @deprecated 无用
 */
export function getI18nResourceVersion(lang: LANG = getCurrentLang(), websiteId: string = '') {
	throw new Error('remove！');
}

/**
 * @deprecated 无用
 */
export function setI18nResourceVersion(version, lang: LANG = getCurrentLang(), websiteId: string = ''): void {
	throw new Error('remove！');
}

export function getI18nResourceKey(lang: LANG = getCurrentLang(), websiteId: string = ''): string {
	return I18N_KEY.I18N_RESOURCE + [websiteId, lang].join('.');
}

export function getI18nResource(lang: LANG = getCurrentLang(), websiteId: string = ''): any {
	const resourceKey = getI18nResourceKey(lang, websiteId);
	return getFromLocal(resourceKey);
}

export function setI18nResource(lang: LANG = getCurrentLang(), i18nLangData, websiteId: string = ''): void {
	const resourceKey = getI18nResourceKey(lang, websiteId);
	return set2Local(resourceKey, i18nLangData);
}

/**
 * @deprecated 无用
 */
export async function initI18nResource(lang: LANG, websiteId: string = '', timeout?: number): Promise<void> {
	throw new Error('remove！');
}

let collection: Map<string, string> = null;
export function startCollect() {
	collection = new Map();
}

export function stopCollect() {
	const result = Object.fromEntries(collection.entries());
	collection = null;
	return result;
}

export function i18n(key, lang: LANG = getCurrentLang(), websiteId: string = ''): string {
	key = key.trim();
	const i18nResource = getI18nResource(lang, websiteId) || {};
	const result = i18nResource[key];
	if (result !== undefined) collection?.set(key, i18nResource[key]);
	return result ?? key;
}

// search value, 通过 value 的一部分查找键值对
global.sv = (value2FindKey, lang: LANG = getCurrentLang(), websiteId: string = '') => {
	let i18nResource = getI18nResource(lang, websiteId);
	forEach(i18nResource, (value, key) => {
		if (new RegExp(value2FindKey, 'i').test(value)) {
			console.debug(key + ' : ' + value);
		}
	});
};

// search key, 通过 key 的一部分查找键值对
global.sk = (key2FindValue, lang: LANG = getCurrentLang(), websiteId: string = '') => {
	let i18nResource = getI18nResource(lang, websiteId);
	forEach(i18nResource, (value, key) => {
		if (new RegExp(key2FindValue, 'i').test(key)) {
			console.debug(key + ' : ' + value);
		}
	});
};
